//
//  MapCenterCalloutView.m
//  MyAlert
//
//  Created by 美鑫科技 on 16/5/3.
//  Copyright © 2016年 美鑫科技. All rights reserved.
//

#import "MapCenterCalloutView.h"
#define kArrorHeight              6

@interface MapCenterCalloutView ()

@property (nonatomic, strong) UILabel *lblTitle;
@property (nonatomic, strong) CAShapeLayer *maskLayer;

@end

@implementation MapCenterCalloutView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.lblTitle.textAlignment = NSTextAlignmentCenter;
        self.lblTitle.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.lblTitle];
        self.backgroundColor = [UIColor whiteColor];
        [self addLayerMask];
    }
    return self;
}

- (void)addLayerMask{
    UIBezierPath *path = [UIBezierPath bezierPath];
    CGFloat maxX = CGRectGetMaxX(self.frame);
    CGFloat maxY = CGRectGetMaxY(self.frame) - 8.0;
    CGFloat midX = CGRectGetMidX(self.frame);
    
    [path moveToPoint:CGPointMake(midX, maxY + 8.0)];
    [path addLineToPoint:CGPointMake(midX + 8.0, maxY)];
    [path addLineToPoint:CGPointMake(maxX, maxY)];
    [path addLineToPoint:CGPointMake(maxX, 0)];
    [path addLineToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(0, maxY)];
    [path addLineToPoint:CGPointMake(midX - 8.0, maxY)];
    [path closePath];
    self.maskLayer.path = [path CGPath];
    self.layer.mask = self.maskLayer;
}

- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    self.lblTitle.frame = CGRectMake(0, 0, frame.size.width, frame.size.height - 10);
    [self addLayerMask];
}

- (void)setTitle:(NSString *)title{
    if (_title != title) {
        _title = title;
    }
    self.lblTitle.text = title;
}

- (CAShapeLayer *)maskLayer{
    if (!_maskLayer) {
        _maskLayer = [CAShapeLayer layer];
    }
    return _maskLayer;
}

@end
